package com.chenwut.List_Set_Map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        //创建集合对象
        Map<String, String> map = new HashMap<String, String>();

        //V put(K key,V value)：添加元素
        map.put("张无忌", "赵敏");
        map.put("郭靖", "黄蓉");
        map.put("杨过", "小龙女");
        map.put("abc", "jska");
        map.put("acD", "jska");

        //V remove(Object key)：根据键删除键值对元素
//        System.out.println(map.remove("郭靖"));
//        System.out.println(map.remove("郭襄"));

        //void clear()：移除所有的键值对元素
//        map.clear();

        //boolean containsKey(Object key)：判断集合是否包含指定的键
//        System.out.println(map.containsKey("郭靖"));
//        System.out.println(map.containsKey("郭襄"));

        //boolean isEmpty()：判断集合是否为空
//        System.out.println(map.isEmpty());

        //int size()：集合的长度，也就是集合中键值对的个数
        System.out.println(map.size());

        System.out.println("----------------------------------------遍历方式一：-----------键找值");
        //遍历方式一：-----------键找值
        //Lambda表达式
        System.out.println("Lambda表达式");
        Set<String> keySet = map.keySet();
        keySet.forEach(s -> {
            String value = map.get(s);
            System.out.println(s + " : " + value);

        });
        System.out.println("=====================================");


        //增强for
        System.out.println("增强for");
        for (String s : keySet) {
            String value = map.get(s);
            System.out.println(s + " : " + value);
        }
        System.out.println("=====================================");


        //迭代器
        System.out.println("迭代器");
        Iterator<String> it1 = keySet.iterator();
        while (it1.hasNext()) {
            String key = it1.next();
            String value = map.get(key);
            System.out.println(key + " : " + value);
        }
        System.out.println("=====================================");


        System.out.println("----------------------------------------遍历方式二：-----------键值对");
        //遍历方式二：-----------键值对
        //迭代器
        System.out.println("迭代器");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> it2 = entries.iterator();
        while (it2.hasNext()) {
            System.out.println(it2.next());
        }
        System.out.println("=====================================");

        //增强for
        System.out.println("增强for");
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry);
        }
        System.out.println("=====================================");

        //Lambda表达式
        System.out.println("Lambda表达式");
        entries.forEach(stringStringEntry -> System.out.println(stringStringEntry));


        System.out.println("----------------------------------------遍历方式三：-----------Lambda表达式");
        map.forEach((key, value) -> System.out.println(key + " = " + value));

        //输出集合对象
        System.out.println(map);
    }
}
